/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package colibri.util;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**Cria uma Scroll Bar.
 *
 * @author Bayron Thiengo Quinelato
 */
public class ImageAction {

    public static Image createThumbnail(Image image) {
        int sourceWidth = image.getWidth();
        int sourceHeight = image.getHeight();

        int thumbWidth = 64;
        int thumbHeight = -1;

        if (thumbHeight == -1)
            thumbHeight = thumbWidth * sourceHeight / sourceWidth;

        Image thumb = Image.createImage(thumbWidth, thumbHeight);
        Graphics g = thumb.getGraphics();

        for (int y = 0; y < thumbHeight; y++) {
            for (int x = 0; x < thumbWidth; x++) {
                g.setClip(x, y, 1, 1);
                int dx = x * sourceWidth / thumbWidth;
                int dy = y * sourceHeight / thumbHeight;
                g.drawImage(image, x - dx, y - dy, Graphics.LEFT | Graphics.TOP);
            }
        }

        Image immutableThumb = Image.createImage(thumb);

        return immutableThumb;
    }

    public static Image createThumbnail(Image image, int thumbWidth) {
        if (image == null)
            return null;
        if (thumbWidth <= 0)
            return image;

        int sourceWidth = image.getWidth();
        int sourceHeight = image.getHeight();

        int thumbHeight = -1;

        if (thumbHeight == -1)
            thumbHeight = thumbWidth * sourceHeight / sourceWidth;

        Image thumb = Image.createImage(thumbWidth, thumbHeight);
        Graphics g = thumb.getGraphics();

        for (int y = 0; y < thumbHeight; y++) {
            for (int x = 0; x < thumbWidth; x++) {
                g.setClip(x, y, 1, 1);
                int dx = x * sourceWidth / thumbWidth;
                int dy = y * sourceHeight / thumbHeight;
                g.drawImage(image, x - dx, y - dy, Graphics.LEFT | Graphics.TOP);
            }
        }

        Image immutableThumb = Image.createImage(thumb);

        return immutableThumb;
    }

    public static Image createThumbnail(Image image, int thumbWidth, int thumbHeight) {
        if (image == null)
            return null;
        if (thumbWidth <= 0 || thumbHeight <= 0)
            return image;

        int sourceWidth = image.getWidth();
        int sourceHeight = image.getHeight();

        Image thumb = Image.createImage(thumbWidth, thumbHeight);
        Graphics g = thumb.getGraphics();

        for (int y = 0; y < thumbHeight; y++) {
            for (int x = 0; x < thumbWidth; x++) {
                g.setClip(x, y, 1, 1);
                int dx = x * sourceWidth / thumbWidth;
                int dy = y * sourceHeight / thumbHeight;
                g.drawImage(image, x - dx, y - dy, Graphics.LEFT | Graphics.TOP);
            }
        }

        Image immutableThumb = Image.createImage(thumb);

        return immutableThumb;
    }

    public static Image ZoomIn(Image img) {
        Image outImg = null;
        int[] rgbOutput = null;

        Graphics g = null;
        int width = 0, height = 0;

        try {
            width = img.getWidth();
            height = img.getHeight();
            outImg = Image.createImage(height, width);
            int rgbInput[] = new int[width * height];
            rgbOutput = new int[width * height];
            img.getRGB(rgbInput, 0, width, 0, 0, width, height);

            int i, j, k;
            k = 0;
            int tempArr[][] = new int[height][width];

            for (i = 0; i < height; i++) {
                for (j = 0; j < width; j++) {
                    tempArr[i][j] = rgbInput[k++];
                }
            }

            k = 0;
            for (i = 0; i < height; i++) {
                for (j = 0; j < width; j++) {
                    rgbOutput[k] = tempArr[j][i];
                    k++;
                }
            }
            g = outImg.getGraphics();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Image.createRGBImage(rgbOutput, height, width, true);
    }

    public static Image zoomOut(Image img) {
        Image outImg = null;

        int[] rgbOutput = null;
        Graphics g = null;
        int width = 0, height = 0;

        try {
            width = img.getWidth();
            height = img.getHeight();

            outImg = Image.createImage(width << 1, height << 1);

            int rgbInput[] = new int[width * height];
            rgbOutput = new int[(width << 1) * (height << 1)];

            img.getRGB(rgbInput, 0, width, 0, 0, width, height);

            int i, j, k;
            k = 0;
            for (i = 0; i < (height << 1); i += 2) {
                for (j = 0; j < (width << 1); j += 2) {
                    rgbOutput[i * (width << 1) + j] = rgbInput[k];
                    rgbOutput[(i + 1) * (width << 1) + j] = rgbInput[k];
                    rgbOutput[i * (width << 1) + j + 1] = rgbInput[k];
                    rgbOutput[(i + 1) * (width << 1) + j + 1] = rgbInput[k];
                    k++;
                }
            }

            g = outImg.getGraphics();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Image.createRGBImage(rgbOutput, width << 1, height << 1, true);
    }
}
